using NodeCanvas.Variables;
using UnityEngine;
using UnityEngine.AI;

namespace NodeCanvas.Actions
{
	[Category("Movement")]
	[AgentType(typeof(NavMeshAgent))]
	[Description("Move randomly between various game object positions taken from the list provided")]
	public class MoveToFromList : ActionTask
	{
		[RequiredField]
		public BBGameObjectList targetList = new BBGameObjectList
		{
			useBlackboard = true
		};

		public BBFloat speed = new BBFloat
		{
			value = 3f
		};

		public float keepDistance = 0.1f;

		private int index;

		private Vector3 lastRequest;

		private NavMeshAgent navAgent
		{
			get
			{
				return (NavMeshAgent)base.agent;
			}
		}

		protected override string info
		{
			get
			{
				return "Go Random " + targetList;
			}
		}

		protected override void OnExecute()
		{
			int num;
			for (num = Random.Range(0, targetList.value.Count); num == index; num = Random.Range(0, targetList.value.Count))
			{
			}
			index = num;
			GameObject gameObject = targetList.value[index];
			if (gameObject == null)
			{
				Debug.LogWarning("List's game object is null on MoveToFromList Action");
				EndAction(false);
				return;
			}
			Vector3 position = gameObject.transform.position;
			navAgent.speed = speed.value;
			if ((navAgent.transform.position - position).magnitude < navAgent.stoppingDistance + keepDistance)
			{
				EndAction(true);
			}
			else
			{
				Go();
			}
		}

		protected override void OnUpdate()
		{
			Go();
		}

		private void Go()
		{
			Vector3 position = targetList.value[index].transform.position;
			if (lastRequest != position && !navAgent.SetDestination(position))
			{
				EndAction(false);
				return;
			}
			lastRequest = position;
			if (!navAgent.pathPending && navAgent.remainingDistance <= navAgent.stoppingDistance + keepDistance)
			{
				EndAction(true);
			}
		}

		protected override void OnStop()
		{
			lastRequest = Vector3.zero;
			if (navAgent.gameObject.activeSelf)
			{
				navAgent.ResetPath();
			}
		}

		protected override void OnPause()
		{
			OnStop();
		}

		protected override void OnGizmosSelected()
		{
			if (!base.agent || targetList.value == null)
			{
				return;
			}
			foreach (GameObject item in targetList.value)
			{
				if ((bool)item)
				{
					Gizmos.DrawSphere(item.transform.position, 0.1f);
				}
			}
		}
	}
}
